package com.brothers.thisGame.basic;

import java.util.Observable;
import org.slf4j.LoggerFactory;
import com.brothers.thisGame.Game;
import com.brothers.thisGame.Player;

public class BasicPlayer implements Player {

	final static org.slf4j.Logger LOGGER = LoggerFactory
			.getLogger(BasicPlayer.class);

	Integer id;
	String nick;
	int remaining;

	public BasicPlayer(Integer id, String nick) {
		this.id = id;
		this.nick = nick;
	}

	public void buy(int score) {
		remaining = remaining + score;
		LOGGER.debug(String.format("%s购买了%d个筹码，现在共有%d个筹码。", nick, score,
				remaining));
	}

	public void cash(int score) {
		remaining = remaining - score;
		LOGGER.debug(String.format("%s兑现了%d个筹码，现在共有%d个筹码。", nick, score,
				remaining));
	}



	@Override
	public int getRemaining() {
		return remaining;
	}

	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub

	}

	public void quit(Game game) {
		game.getBettingTable().remove(this);
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getNick() {
		return nick;
	}

	public void setNick(String nick) {
		this.nick = nick;
	}

	@Override
	public void setRemaining(int remaining) {
		this.remaining = remaining;
	}

	@Override
	public Game makeGame(int odds, int totalScore) {
		Game game = new BasicGame();
		game.setMaker(this);
		game.setOdds(odds);
		game.setTotalScoreLimit(totalScore);
		return game;
	}


	public void bet(Game game, int score) {
		int myLastBet = game.getBettingTable().get(this)==null?0:game.getBettingTable().get(this).intValue();
		game.getBettingTable().put(this, myLastBet + score);
		remaining = remaining - score;

		LOGGER.debug(String.format("%s下注了%d个筹码。",
				nick,score));
	}
	@Override
	public void gain(int income) {
		remaining = remaining + income;
	}

	@Override
	public void lose(int outcome) {
		remaining = remaining - outcome;
	}

}
